package mes

import (
	"context"
	"encoding/json"
	"ez/apps/mes/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Check struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Plan string `json:"plan" bson:"plan"` //计划

	Sn string `json:"sn" bson:"sn"` //条码

	Flow *Flow `json:"flow" bson:"flow"` //跟踪单

	FlowId int64 `json:"flowId" bson:"flowId"` //跟踪单

	Process *CheckProcess `json:"process" bson:"process"` //检验工序

	ProcessId int64 `json:"processId" bson:"processId"` //检验工序

	Num float64 `json:"num" bson:"num"` //成品

	NumX float64 `json:"numX" bson:"numX"` //修品

	NumH float64 `json:"numH" bson:"numH"` //焊品

	NumF float64 `json:"numF" bson:"numF"` //废品

	NumSum float64 `json:"numSum" bson:"numSum"` //检验总数

	Emp *Emp `json:"emp" bson:"emp"` //检验员

	EmpId int64 `json:"empId" bson:"empId"` //检验员

	Results []*CheckResult `json:"results" bson:"results"` //检验结果

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *Check) DocName() string { return "Check" }

func (this *Check) GetId() int64 { return this.Id }

func (this *Check) SetId(id int64) { this.Id = id }

func (this *Check) Create() error {
	return this.GetFactory().Create(this)
}

func (this *Check) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *Check) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Check) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *Check) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Check) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *Check) ToString() string {
	return string(this.ToBytes())
}

func (this *Check) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Check) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Check) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *Check) LoadFlow() {
	if this.FlowId == 0 {
		return
	}
	this.Flow, _ = NewFlowCrud().FindId(this.FlowId)
}

func (this *Check) LoadProcess() {
	if this.ProcessId == 0 {
		return
	}
	this.Process, _ = NewCheckProcessCrud().FindId(this.ProcessId)
}

func (this *Check) LoadEmp() {
	if this.EmpId == 0 {
		return
	}
	this.Emp, _ = NewEmpCrud().FindId(this.EmpId)
}

func (this *Check) LoadResults() {
	results, _ := NewCheckResultCrud().FindBy(ss.M{"resultsIds": this.Id}, []string{"id"}, 0, 0)
	this.Results = results
}

func (this *Check) ClearRelationsBeforeSave() mgo.Doc {

	this.Flow = nil

	this.Process = nil

	this.Emp = nil

	this.Results = nil

	return this
}

func neverUsed_Check() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type CheckAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Check
	Session *ez.Session
}

func NewCheckAccessControl(model *Check, action string, session *ez.Session) *CheckAccessControl {
	ctrl := &CheckAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.CheckAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
